module example1
    use ISO_C_binding
    implicit none
    private
    public :: pointercf

    type pointercf
        ! real(c_double), dimension(:,:), pointer :: part3d_in
        real(c_double), dimension(:), pointer :: part3d_in
        ! integer(c_int) :: dim1, dim2
        integer :: dim
    contains
        generic :: go1 => go
        generic :: new => init
        generic :: del => end
        procedure, private :: go
        procedure, private :: init, end
    end type pointercf

    contains

    subroutine init(this)
        implicit none
        class(pointercf), intent(inout) :: this

        ! allocate(this%part3d_in(this%dim1, this%dim2))
        allocate(this%part3d_in(this%dim))

    end subroutine init

    ! subroutine go(this, part3d_in, dim1, dim2)
    subroutine go(this,part3d_in,dim)
        implicit none
        class(pointercf), intent(inout) :: this
        ! integer(c_int), intent(inout) :: dim1, dim2
        integer(c_int), intent(in) :: dim
        ! integer(c_int), value, intent(in) :: dim
        type(c_ptr), value, intent(in) :: part3d_in
        ! real(c_double), dimension(:,:), pointer :: part3d_in_f
        real(c_double), dimension(:), pointer :: part3d_in_f
        ! integer :: i,j
        integer :: i, num_particles, num_params_per_particle
        real(c_double) :: tempz, tempx, tempy, tempvz, tempvx, tempvy, tempq

        call this%new()
        ! this%dim1 = dim1
        ! this%dim2 = dim2
        this%dim = dim
        ! call c_f_pointer(part3d_in, part3d_in_f, [dim1, dim2])
        call c_f_pointer(part3d_in, part3d_in_f, [this%dim])
        this%part3d_in = part3d_in_f
        ! print *, "Array values:"
        ! do i = 1, dim1
        !     do j = 1, dim2
        !     print *, "part3d_in(",i,",",j,") = ", this%part3d_in(i, j)
        !     end do
        ! end do

        ! 假设每个粒子有 7 个参数
        num_params_per_particle = 7
        num_particles = this%dim / num_params_per_particle
        ! 遍历每个粒子并获取参数
        do i = 1, num_particles
            tempz  = part3d_in_f((i-1)*num_params_per_particle + 1)
            tempx  = part3d_in_f((i-1)*num_params_per_particle + 2)
            tempy  = part3d_in_f((i-1)*num_params_per_particle + 3)
            tempvz = part3d_in_f((i-1)*num_params_per_particle + 4)
            tempvx = part3d_in_f((i-1)*num_params_per_particle + 5)
            tempvy = part3d_in_f((i-1)*num_params_per_particle + 6)
            tempq  = part3d_in_f((i-1)*num_params_per_particle + 7)

            print *, "粒子", i, "的参数:"
            print *, "tempz =", tempz
            print *, "tempx =", tempx
            print *, "tempy =", tempy
            print *, "tempvz =", tempvz
            print *, "tempvx =", tempvx
            print *, "tempvy =", tempvy
            print *, "tempq =", tempq
        end do

        call this%del()
        
    end subroutine go

    subroutine end(this)
        implicit none
        class(pointercf), intent(inout) :: this

        if (associated(this%part3d_in)) then
            deallocate(this%part3d_in)
        end if
    end subroutine end

end module example1